package com.hcss.common.redis.base.impl;

import com.hcss.common.redis.base.BaseRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @描述:
 * @作者: xuqian
 * @创建日期: 2018-01-15 13:37
 * @公司 浙江鸿程计算机系统有限公司
 */
@Repository("baseRedisDao")
public abstract class BaseRedisDaoImpl<T> implements BaseRedisDao<T> {

    @Qualifier("getRedisTemplate")
    private final RedisTemplate redisTemplate;

    @Autowired
    public BaseRedisDaoImpl(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    @Override
    public void clear(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void clear(Collection keys) {
        redisTemplate.delete(keys);
    }

    @Override
    public void clearAll(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @Override
    public void setTime(String key, long time, TimeUnit unit) {
        redisTemplate.expire(key, time, unit);
    }
}
